package com.example.springcloud.service.impl;

import java.util.List;

import com.example.springcloud.mapper.CategoriesMapper;
import com.example.springcloud.mapper.RegionsMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.springcloud.mapper.ProductsMapper;
import com.example.springcloud.domain.Products;
import com.example.springcloud.service.IProductsService;

/**
 * 商品信息Service业务层处理
 * 
 * @author hx
 * @date 2025-02-14
 */
@Service
public class ProductsServiceImpl implements IProductsService 
{
    @Autowired
    private ProductsMapper productsMapper;

    @Autowired
    private RegionsMapper regionsMapper;

    @Autowired
    private CategoriesMapper categoriesMapper;

    /**
     * 查询商品信息
     * 
     * @param productId 商品信息主键
     * @return 商品信息
     */
    @Override
    public Products selectProductsByProductId(Long productId)
    {
        return productsMapper.selectProductsByProductId(productId);
    }

    /**
     * 查询商品信息列表
     * 
     * @param products 商品信息
     * @return 商品信息
     */
    @Override
    public List<Products> selectProductsList(Products products)
    {
        List<Products> productsList = productsMapper.selectProductsList(products);
        productsList.forEach(product -> {
            // 分类名称和地区名称
            product.setRegionName(regionsMapper.selectRegionsByRegionId(product.getRegionId()).getRegionName());
            product.setCategoryName(categoriesMapper.selectCategoriesByCategoryId(product.getCategoryId()).getCategoryName());
        });

        return productsList;
    }

    /**
     * 新增商品信息
     * 
     * @param products 商品信息
     * @return 结果
     */
    @Override
    public int insertProducts(Products products)
    {
        return productsMapper.insertProducts(products);
    }

    /**
     * 修改商品信息
     * 
     * @param products 商品信息
     * @return 结果
     */
    @Override
    public int updateProducts(Products products)
    {
        return productsMapper.updateProducts(products);
    }

    /**
     * 批量删除商品信息
     * 
     * @param productIds 需要删除的商品信息主键
     * @return 结果
     */
    @Override
    public int deleteProductsByProductIds(Long[] productIds)
    {
        return productsMapper.deleteProductsByProductIds(productIds);
    }

    /**
     * 删除商品信息信息
     * 
     * @param productId 商品信息主键
     * @return 结果
     */
    @Override
    public int deleteProductsByProductId(Long productId)
    {
        return productsMapper.deleteProductsByProductId(productId);
    }
}
